﻿/*将导入到图库的图片素材，按照文件夹规则，分别生成组件，并把对应的动画帧插入到时间轴*/

/*生成组件*/
var doc = fl.getDocumentDOM();

var lib = doc.library;

//lib.selectAll();

var items = lib.getSelectedItems();

var tempx=0, tempy=0, maxColumnWidth=0, totalWidth=0, totalHeight; 

/*for(var i=0;i < items.length;i++){
	var nowItem = items[i];
	fl.trace(nowItem.name);
	fl.trace(nowItem.itemType);
}
fl.trace("splices");
items.splice(0,1);
for(var i=0;i < items.length;i++){
	var nowItem = items[i];
	fl.trace(nowItem.name);
	fl.trace(nowItem.itemType);
}*/

//不用递归了，查出所有的图片，然后分组调用。----想不明白递归
/*var itemsCopy = items.slice(0);
var picNumBeginIndex = -1;
var picNumEndIndex = -1;
for(var i = 0;i<items.length;i++){
	var item = items[i];
	if(item.itemType == "bitmap"){
			picNumBeginIndex ++;
	}else if(item.itemType == "folder" || i==items.length-1){
			picNumEndIndex = i;
	}
	
	if(picNumBeginIndex >= 0 && picNumEndIndex == i){
		
		fl.trace("调用一次");
		makeSymbol(items.splice(picNumBeginIndex,picNumEndIndex-picNumBeginIndex + 1));
		picNumBeginIndex = -1;
		picNumEndIndex = -1;
	}
	fl.trace("begin=" + picNumBeginIndex);
	fl.trace("end=" + picNumEndIndex);
	
}*/
makeSymbol(items);
/*
	新建元件，并填补动画帧
	如果是图片，执行创建元件，，如果是文件夹，将子集全部选中，然后再递归调用此方法
*/
function makeSymbol(picItems){
	
	
	var item = picItems[0];

	if(item.itemType == "bitmap"){
			
			/*for(var i = 0;i < picItems.length;i++){
		
				var item= picItems[i];
				fl.trace(item.name);
			}
			fl.trace("------------");
			return;	*/
			var newName = item.name.substring(0,item.name.length-4);
			lib.addItemToDocument({x:tempx, y:tempy},item.name);
		
			// 转换为元件
			doc.convertToSymbol('movie clip', newName, 'top left');
			lib.selectItem(newName,false);

			if (lib.getItemProperty('linkageImportForRS') == true) 
			{
					lib.setItemProperty('linkageImportForRS', false);
			}
			lib.setItemProperty('linkageExportForAS', true);
			lib.setItemProperty('linkageExportForRS', false);
			lib.setItemProperty('linkageExportInFirstFrame', true);
			var nameArray = newName.split("/");
			lib.setItemProperty('linkageClassName', nameArray[nameArray.length -3] + "_0_" + nameArray[nameArray.length -2]);
			lib.setItemProperty('scalingGrid',  false);
			var nowItemsSelected = lib.getSelectedItems();
			var symbolItem = nowItemsSelected[nowItemsSelected.length -1];
			var timeline = symbolItem.timeline; 
			//timeline.addNewLayer();

			timeline.removeFrames(0, timeline.frameCount)

		
			
			lib.addItemToDocument({x:0, y:0},symbolItem.name);
			doc.enterEditMode("inPlace"); 
			//for(var j=0;j<itemsSelected.length;j++){
			var frameKey = 1;//1拍几，就对应几
			fl.trace(item.name);
			if(item.name.indexOf("/attack/") > -1){
				frameKey = 1;
			}else if(item.name.indexOf("/death/") > -1){
				frameKey = 2;
			}else if(item.name.indexOf("/stand/") > -1){
				frameKey = 3;
			}
			for(var j=0;j<picItems.length;j++){
				var item = picItems[j];
				
				var multiJ = j * frameKey;
				if(item){
					if(item.name.indexOf("/attack/") > -1){
						
					}
					timeline.setSelectedFrames(multiJ, multiJ);
					timeline.insertBlankKeyframe(multiJ);
					timeline.setSelectedFrames(multiJ, multiJ);
					lib.addItemToDocument({x:0,y:0},item.name)
				}
			}
			timeline.insertFrames(frameKey-1);
			timeline.setSelectedFrames(0,0);//图片插入完成，返回第一帧
			
			doc.exitEditMode();//退出编辑模式

			
	}
	
	for(var i = 0;i < picItems.length;i++){
		var item = picItems[i];
		//fl.trace("itemname = " + item.name)
		if(item.itemType == "folder"){//选择下一级目录，并去掉第一个
			lib.selectItem(item.name);
			var subItems = lib.getSelectedItems();
			if(subItems.length >=2){
					var secondItem = subItems[1];
			
					if(secondItem.itemType == "bitmap"){
						makeSymbol(subItems.slice(1,picItems.length -1));
					}
			}
			
		}
	}
	
}




doc.save();
//doc.selectAll();
//doc.deleteSelection();